<?php
/**
 * Created by PhpStorm.
 * User: jochen
 * Date: 2017/1/5
 * Time: 下午2:00
 */

namespace EWeiLink\JuPai;

use EWeiLink\JuPai\Http\Controllers\ApiController;
use EWeiLink\JuPai\Http\Controllers\ContactsController;
use EWeiLink\JuPai\Http\Controllers\CustomerController;
use EWeiLink\JuPai\Http\Controllers\MessagesController;
use EWeiLink\JuPai\Http\Controllers\PlannerController;
use EWeiLink\JuPai\Http\Controllers\SupportController;
use Illuminate\Contracts\Routing\Registrar as Router;

class RouteRegistrar
{
    /**
     * @var Router
     */
    protected $router;

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function all()
    {
        $this->forApiAuth();
        $this->forPublic();
        $this->forWebAuth();
    }

    /**
     * 注册session认证用路由
     */
    public function forWebAuth()
    {
        $this->router->group(['middleware' => ['web', 'auth', 'json']], function (Router $router) {
            $this->forResource($router);
            $router->delete('contacts/{contact_id}/bind', ContactsController::class . '@unbind');
            $router->post('relations', ContactsController::class . '@relations');
            $router->get('contacts/{contact_id}/messages', ContactsController::class . '@contactMessage'); //指定联系人的对应聊天消息
            $router->get('planners/{planner_id}/contacts', PlannerController::class . '@contacts');
        });
        $this->router->group(['middleware' => ['web', 'auth']], function (Router $router) {
            $this->registerFrontRouter($router);
        });
    }

    /**
     * 注册公共路由，不需要登录认证
     */
    public function forPublic()
    {
        $this->router->post('/api/auth', [
            'middleware' => ['json'],
            'uses' => SupportController::class . '@auth',
        ]);
        $this->router->get('/qrcode', [
            'uses' => ApiController::class . '@qrcode',
        ]);
    }

    /**
     * 注册api认证用路由
     */
    public function forApiAuth()
    {
        $this->router->group([
            'middleware' => ['api', 'auth:api', 'json'],
            'prefix' => 'api/',
        ], function (Router $router) {
            $this->forResource($router);
            $router->post('contacts/{contact_id}/bind', ContactsController::class . '@bind');
            $router->post('init', SupportController::class . '@initOptions');
            $router->get('messages/listen', MessagesController::class . '@listenMessages'); //监听消息
        });
    }

    protected function forResource(Router $router)
    {
        $router->resource('contacts', ContactsController::class);
        $router->resource('planners', PlannerController::class);
        $router->resource('messages', MessagesController::class, ['except' => ['show']]);
        $router->resource('customers', CustomerController::class);
    }

    protected function registerFrontRouter(Router $router)
    {
        $routerList = ['customer', 'message'];
        array_walk($routerList, function($v) use ($router) {
            $router->get($v, '\App\Http\Controllers\HomeController@index');
        });
    }
}